package com.cdream.basic.algorithm.leetcode.findElement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cdream
 * @date 5/30/21
 */
public class Question447 {
    public int numberOfBoomerangs(int[][] points) {
        int count = 0;
        Map<Integer, Integer> mp = new HashMap<>();
        for (int i = 0; i < points.length; i++) {
            mp.clear();
            for (int j = 0; j < points.length; j++) {
                if (i == j) {
                    continue;
                }
                mp.merge(distance(points[j], points[i]), 1, (a1, a2) -> a1 + a2);
            }
            for (Integer value : mp.values()) {
                count += value * (value - 1);
            }
        }
        return count;
    }

    public int distance(int[] p1, int[] p2) {
        return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
    }
}
